package copyengine.dataLayer.rpc
{
	import copyengine.CopyEngine;
	import copyengine.dataLayer.rpc.moudle.codeConvert.ICERpcCodeConvertModule;
	import copyengine.dataLayer.rpc.moudle.errorHandle.ICERpcErrorHandleModule;
	import copyengine.dataLayer.rpc.moudle.idMapping.ICERpcIdMappingModule;
	import copyengine.dataLayer.rpc.moudle.request.ICERpcRequestModule;
	import copyengine.dataLayer.rpc.moudle.response.ICERpcResponseModule;
	import copyengine.dataLayer.rpc.moudle.secret.ICERpcSecretModule;
	import copyengine.utils.debug.CELog;

	public final class CERpcManger implements ICERpcManger
	{
		private var codeConvert:ICERpcCodeConvertModule;
		private var errorHandle:ICERpcErrorHandleModule;
		private var idMapping:ICERpcIdMappingModule;
		private var request:ICERpcRequestModule;
		private var response:ICERpcResponseModule;
		private var secret:ICERpcSecretModule;

		private var requestCount:Number;

		/**
		 * 临时存储回调DataArray
		 */
		private var tempDataArray:Array;

		private var isRpcInError:Boolean = false;

		private var finishCallBack:Function;

		public function CERpcManger()
		{
		}

		public function initialize(_codeConvert:ICERpcCodeConvertModule,
								   _errorHandle:ICERpcErrorHandleModule,
								   _idMapping:ICERpcIdMappingModule,
								   _request:ICERpcRequestModule,
								   _response:ICERpcResponseModule,
								   _secret:ICERpcSecretModule):void
		{
			codeConvert = _codeConvert;
			errorHandle = _errorHandle;
			idMapping = _idMapping;
			request = _request;
			response = _response;
			secret = _secret;

			requestCount = 0;
		}

		public function sendRequest(_logicLayerDataArray:Array, _finishCallBack:Function):void
		{
			if (!isRpcInError)
			{
				finishCallBack = _finishCallBack;
				var transferLayerData:*;
				_logicLayerDataArray = idMapping.convertLogicLayerIDToTransferLayerID(_logicLayerDataArray);
				transferLayerData = codeConvert.logicLayerDataToTransferLayerData(_logicLayerDataArray, requestCount);
				transferLayerData = secret.encrypt(transferLayerData, requestCount);
				request.sendRpcRequest(transferLayerData);
				requestCount++;
			}
		}

		public function sendRequestFailed():void
		{
			CELog.err("Can not send rpcRequest , execute Error handle flow");
			if (CopyEngine.DEBUG_RPC_MOUDLE)
			{
				//在Debug情况下认为请求均送达
				finishCallBack && finishCallBack();
			}
			else
			{
				isRpcInError = true;
			}
			errorHandle.sendRequestFailedErrorFlow();
		}

		public function receiveServerRespondData(_transferLayerData:*):void
		{
			tempDataArray = codeConvert.transferLayerDataToLogicLayerData(secret.decrypt(_transferLayerData));
			if (tempDataArray == null)
			{
				CELog.err("Can not analysis server data , execute Error handle flow");
				if (CopyEngine.DEBUG_RPC_MOUDLE)
				{
					//在Debug情况下认为请求均送达
					finishCallBack && finishCallBack();
				}
				else
				{
					isRpcInError = true;
				}
				errorHandle.analyseServerDataFailedErrorFlow();
			}
			else
			{
				tempDataArray = idMapping.convertTransferLayerIDToLogicLayerID(tempDataArray);
				response.sendRpcRespond(tempDataArray);
				finishCallBack && finishCallBack();
			}
		}

	}
}
